#coding=utf-8
import unittest
import os
import sys
import time
import  random
from xlwt import *
import xlrd
from xlutils.copy import copy

class excel_Operate():
    def create_excel(self):
        excel_path = 'd://'
        excel_name_base = 'test-'
        timestr = time.strftime('%Y-%m-%d', time.localtime(time.time()))  # 本地日期时间作为测试报告的名字
        randomStr = str(random.uniform(5, 500))
        excel_name = excel_path + excel_name_base + timestr + randomStr + '.csv'
        print  'excel_name is '+ str(excel_name)
        excel_file = Workbook(encoding='utf-8')
        ws = excel_file.add_sheet("result")

        excel_file.save(excel_name)
        return  excel_name

    def excel_context_op(self,file_name,rowIndex,newValue):

        date_src = xlrd.open_workbook(file_name)
        date_location = copy(date_src)

        newValueLen = len(newValue)
        print 'newValueLen='+str(newValueLen)
        for colIndex in range (0,newValueLen,1):
            # print 'cloinex = '+str(cloinex)
            # print newValue[cloinex]
            date_location.get_sheet(0).write(rowIndex, colIndex, newValue[colIndex])
        date_location.save(file_name)

        return  0

    def CaseDataOpreate(self,file_name):
        #打开excel表格，读取内容
        date_src = xlrd.open_workbook(file_name)
        #获取一个sheet的内容
        case_table = date_src.sheet_by_index(0)
        #获取excel表的行数和列数
        case_table_nrows = case_table.nrows
        case_table_nclos = case_table.ncols
        #定义一个变量，用来接收需要执行的case数据
        case_list = []
        #循环从用来的excel取数据，并进行判断是否可以执行，可执行，则放入list中
        for caseId in range(case_table_nrows):
            table_vale_row = case_table.row_values(caseId)
            case_exe_flag = table_vale_row[4]
            case_value = [table_vale_row[2],table_vale_row[3]]
            if 'YES' == case_exe_flag:
                case_list.append(case_value)
        # print case_list
        # print case_list[1]
        return case_list

    def test(self):
        return 0

if __name__ == '__main__':
    test = excel_Operate()
    # test.create_excel()
    # testValue = [10,11,12,13]
    # test.excel_context_op('d://test-2017-04-17276.532827812.csv',0,testValue)
    test.CaseDataOpreate('D:\Cruise\pythonSrc\egova\Data\TestCaesData.xlsx')
    unittest.main()
